package com.sd.repay.service.impl;

import com.sd.repay.dao.BaseDao;
import com.sd.repay.service.CardManageService;
import com.sd.repay.service.PayChannelService;
import com.sd.repay.utils.DateUtil;
import com.sd.repay.utils.RandomUtil;
import com.sd.repay.utils.StringUtil;
import com.sd.repay.utils.WebUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;

/**
 * @author sd
 * @Description: 通道路由服务
 * @date 2017年10月25日
 */
@Service
public class PayChannelServiceImpl implements PayChannelService {

    private static Logger log = LoggerFactory.getLogger(PayChannelServiceImpl.class);

    @Autowired
    private BaseDao baseDao;
    @Autowired
    private CardManageService cardManageService;

    /**
     * 根据银行编码，还款类型路由通道
     *
     * @param settleBankNo
     * @param repayType
     * @return
     * @throws Exception
     */
    @Override
    public String routePayChannel(String settleBankNo, String repayType) throws Exception {

        log.info("路由通道参数，repayType：" + repayType + "，settleBankNo：" + settleBankNo);

        //获取不支持卡种
        Set<String> excludeChannelSet = new HashSet<>();
        List<Map<String, Object>> excludeCardList = cardManageService.getExcludeCard();
        String excludeSettleBankNo = "", excludeCahnnelCode = "";
        for (Map<String, Object> excludeCardMap : excludeCardList) {
            excludeSettleBankNo = StringUtil.filterNull(excludeCardMap.get("settle_bank_no"));
            excludeCahnnelCode = StringUtil.filterNull(excludeCardMap.get("channel_code"));
            if (StringUtils.isNotEmpty(excludeSettleBankNo) && StringUtils.isNotEmpty(excludeCahnnelCode) && excludeSettleBankNo.equals(settleBankNo)) {
                excludeChannelSet.add(excludeCahnnelCode);
            }
        }

        String acqCode = null;

        int currHour = Integer.parseInt(DateUtil.format(new Date(), "HH"));
        log.info("路由通道当前时间点：" + currHour);

        //还款类型，当前时间点，不支持卡获取可使用的通道
        List<Map<String, Object>> currChannels = WebUtil.getChannelByTypeAndExcludeAndTime(repayType, currHour, excludeChannelSet.toArray());
        if (CollectionUtils.isEmpty(currChannels)) {
            return null;
        }

        if (currChannels.size() == 1) {

            acqCode = StringUtil.filterNull(currChannels.get(0).get("channel_code"));
        } else {

            int maxEndPoint = 1;
            for (Map<String, Object> singleChannel : currChannels) {
                maxEndPoint += Integer.parseInt(StringUtil.filterNull(singleChannel.get("percent")));
            }

            int random = RandomUtil.getRandomInt(0, maxEndPoint);
            int beginPoint = 1;
            int endPoint = beginPoint;
            int channelPercent;

            for (Map<String, Object> singleChannel : currChannels) {

                channelPercent = Integer.parseInt(StringUtil.filterNull(singleChannel.get("percent")));
                endPoint += channelPercent;

                if (random > beginPoint && random <= endPoint) {

                    acqCode = StringUtil.filterNull(singleChannel.get("channel_code"));
                    break;
                }
                beginPoint += channelPercent;
            }
        }
        return acqCode;
    }

    /**
     * 判断通达是否支持此银行卡
     *
     * @param bankCode
     * @param channelCode
     * @return
     * @throws Exception
     */
    @Override
    public boolean isSupportBankByChannel(String bankCode, String channelCode) throws Exception {
        List<Map<String, Object>> mapList = cardManageService.getExcludeCardByChannel(channelCode);
        String excludeSettleBankNo;
        for (Map<String, Object> map : mapList) {
            excludeSettleBankNo = StringUtil.filterNull(map.get("settle_bank_no"));
            if (bankCode.equals(excludeSettleBankNo)) {
                return false;
            }
        }
        return true;
    }
}
